package com.appspot.zcmjqm.web;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.appspot.zcmjqm.app.model.Consult;
import com.appspot.zcmjqm.app.model.Product;
import com.appspot.zcmjqm.app.model.Service;
import com.appspot.zcmjqm.app.model.category.ConsultingCategory;
import com.appspot.zcmjqm.app.model.category.ProductCategory;
import com.appspot.zcmjqm.app.model.category.ServiceCategory;

@Controller
public class DataSetupCotrollerWebMVCImpl {
	
	@Autowired
	private EntityManager entityManager;
	
	private String[] productCategories = new String[]{
			"Desktops"
		  , "Laptops"
		  , "Tablets"
		  , "Accessories"
	};
	
	private String[] serviceCategories = new String[]{
			"Applicaton Development"
		  , "Hosting"
		  , "Code Review"
	};
	
	private String[] consultCategories = new String[]{
			"Requirements Gathering"
		  , "Archiecture Design"
		  , "Database Design"
		  , "Configuration Management"
	};
	
	@RequestMapping("/setupdata.do")
	public void setupData(HttpServletResponse res) throws IOException {
		StringBuffer response = new StringBuffer();
		
		response.append("Removed conslts: " + entityManager.createQuery("delete from Consult").executeUpdate()+ ".\n");
		response.append("Removed services: " + entityManager.createQuery("delete from Service").executeUpdate()+ ".\n");
		response.append("Removed products: " + entityManager.createQuery("delete from Product").executeUpdate()+ ".\n");
		response.append("Removed consulting categories: " + entityManager.createQuery("delete from ConsultingCategory").executeUpdate()+ ".\n");
		response.append("Removed service categories: " + entityManager.createQuery("delete from ServiceCategory").executeUpdate()+ ".\n");
		response.append("Removed product categories: " + entityManager.createQuery("delete from ProductCategory").executeUpdate()+ ".\n");
		
		for (String productCategory : productCategories) {
			ProductCategory pc = new ProductCategory();
			pc.setTitle(productCategory);
			entityManager.merge(pc);
			entityManager.refresh(pc);
			response.append("Added category: " + productCategory + ".\n");
			for (int i = 0; i < 3; i++) {
				Product p = new Product();
				p.setDescription("A very great product. You should buy it because its product " + i + ".");
				p.setPrice(100.00);
				p.setProductCategory(pc);
				p.setTitle(pc.getTitle() + i);
				entityManager.merge(p);
				entityManager.refresh(p);
				response.append("Added item to category ("+pc.getTitle()+": " + p.getTitle() + ".\n");
			}
		}
		
		for (String serviceCategory : serviceCategories) {
			ServiceCategory sc = new ServiceCategory();
			sc.setTitle(serviceCategory);
			entityManager.merge(sc);
			entityManager.refresh(sc);
			response.append("Added category: " + serviceCategory + ".\n");
			for (int i = 0; i < 3; i++) {
				Service s = new Service();
				s.setDescription("A very great product. You should buy it because its product " + i + ".");
				s.setServiceCategory(sc);
				s.setTitle(sc.getTitle() + i);
				entityManager.merge(s);
				entityManager.refresh(s);
				response.append("Added item to category ("+sc.getTitle()+": " + s.getTitle() + ".\n");
			}
		}
		
		for (String consultCategory : consultCategories) {
			ConsultingCategory cc = new ConsultingCategory();
			cc.setTitle(consultCategory);
			entityManager.merge(cc);
			entityManager.refresh(cc);
			response.append("Added category: " + consultCategory + ".\n");
			for (int i = 0; i < 3; i++) {
				Consult c = new Consult();
				c.setDescription("A very great product. You should buy it because its product " + i + ".");
				c.setConsultingCategory(cc);
				c.setTitle(cc.getTitle() + i);
				entityManager.merge(c);
				entityManager.refresh(c);
				response.append("Added item to category ("+cc.getTitle()+": " + c.getTitle() + ".\n");
			}
		}
		
		res.getWriter().write(response.toString());
	}

}
